package com.suxin.logger;

import org.springframework.util.StringUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Hashtable;
import java.util.Properties;

/**
 * @author Tang
 * @classname LogManager
 * @description [  ]
 * @date 2022/4/26 22:12
 */
public class LogManager {

    private static LogManager logManager = null;

    private static LogContext logContext = new LogContext();

    private static Properties prop = new Properties();

    static {
        String cname = System.getProperty("java.util.logging.manager");
        Class<?> aClass = null;
        if (cname != null) {
            try {
                // 1.尝试从外部加载
               aClass = ClassLoader.getSystemClassLoader().loadClass(cname);
               logManager = (LogManager)aClass.newInstance();
            } catch (Exception e) {
                try {
                    aClass = Thread.currentThread().getContextClassLoader().loadClass(cname);
                    logManager = (LogManager)aClass.newInstance();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
        if (aClass == null) {
            logManager = new LogManager();
        }
        readConfiguration();
    }


    public static void readConfiguration() {
        String fName = System.getProperty("java.util.logging.config.file");
        if (!StringUtils.isEmpty(fName)) {
            try {
                FileInputStream fileInputStream = new FileInputStream(fName);
                prop.load(fileInputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    static class LogContext {

        private static Hashtable<String, Logger> nameLoggers = new Hashtable<>();

        public Logger findLogger(String name) {
            return nameLoggers.get(name);
        }

        public boolean addLogContext(Logger logger) {
            nameLoggers.put(logger.getName(), logger);
            return true;
        }

        public Logger getLogContext(Logger logger) {
            return nameLoggers.get(logger.getName());
        }
    }

    public static LogManager getLogManager() {
        return logManager;
    }

    public Logger demandLogger(String name, Class<?> caller) {
        Logger result = getLogger(name);
        if (result == null) {
            result = new Logger(name, caller);
            LogContext userContext = getUserContext();
            if (userContext.addLogContext(result)) {
                loadLoggerHandler(result,name);
            }
        }
        return result;
    }

    private void loadLoggerHandler(Logger result, String name) {
    }

    public String getProperty(String name) {
        return prop.getProperty(name);
    }

    public String getProperty(String name, String defaultValue) {
        if (prop.getProperty(name) == null) {
            return defaultValue;
        }
        return prop.getProperty(name);
    }

    private Logger getLogger(String name) {
        return getUserContext().findLogger(name);
    }

    private LogContext getUserContext() {
        return logContext;
    }

}